package com.sl.biz.modular.datasource.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sl.biz.modular.datasource.controller.request.*;
import com.sl.biz.modular.datasource.dao.domain.dto.ProcessDataSourceDTO;
import com.sl.biz.modular.datasource.dao.domain.entity.ProcessDataSourceDO;
import com.sl.biz.modular.datasource.service.ProcessDatasourceBizService;
import com.sl.core.engine.datasource.DataSourceManager;
import com.sl.core.engine.datasource.domain.dto.*;
import com.sl.core.engine.plugin.domain.PluginDbTypeListDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/process/datasource")
@RequiredArgsConstructor
public class ProcessDatasourceController {

    private final ProcessDatasourceBizService datasourceBizService;

    @PostMapping("/list")
    public List<ProcessDataSourceDTO> list() {
        return datasourceBizService.list();
    }

    @PostMapping("/save")
    public Boolean save(@RequestBody @Validated ProcessDataSourceDO addRequest) {
        return datasourceBizService.save(addRequest);
    }

    @PostMapping("/get_by_id")
    public ProcessDataSourceDTO getById(@RequestBody @Validated ProcessDataSourceDTO addRequest) {
        return datasourceBizService.getById(addRequest);
    }

    @PostMapping("/remove_by_id")
    public Boolean removeById(@RequestBody @Validated ProcessDataSourceDO removeByIdRequest) {
        return datasourceBizService.removeById(removeByIdRequest);
    }

    @PostMapping("/set_status_flag")
    public Boolean setStatusFlag(@RequestBody @Validated ProcessDataSourceDO assertUserUse) {
        return datasourceBizService.setStatusFlag(assertUserUse);
    }

    @PostMapping("/db_type_list")
    public List<PluginDbTypeListDTO> dbTypeList() {
        List<PluginDbTypeListDTO> dbTypeList = DataSourceManager.INSTANCE.getDbTypeList();
        return dbTypeList;
    }


    @PostMapping("/get_url")
    public String getUrl(@RequestBody @Validated ProcessDataSourceDO assertUserUse) {
        return DataSourceManager.INSTANCE.getUrl(BeanUtil.copyProperties(assertUserUse, DataSourceDTO.class));
    }

    @PostMapping("/test_connection")
    public String testConnection(@RequestBody @Validated ProcessDataSourceDO assertUserUse) {
        return datasourceBizService.testConnection(assertUserUse);
    }

    @PostMapping("/query_schema_list")
    public List<String> querySchemaList(@RequestBody @Validated ProcessDataSourceDTO assertUserUse){
        return datasourceBizService.querySchemaList(assertUserUse);
    }
    @PostMapping("/supports_schemas")
    public Boolean supportsSchemas(@RequestBody @Validated ProcessDataSourceDTO assertUserUse){
        return datasourceBizService.supportsSchemas(assertUserUse);
    }
    @PostMapping("/query_catalog_list")
    public List<String> queryCatalogList(@RequestBody @Validated ProcessDataSourceDTO assertUserUse){
        return datasourceBizService.queryCatalogList(assertUserUse);
    }

    @PostMapping("/query_table_list")
    public List<String> queryTableList(@RequestBody @Validated ProcessDataSourceQueryTableListRequest assertUserUse){
        return datasourceBizService.queryTableList(assertUserUse);
    }



    @PostMapping("/query_table_field_list")
    public List<TableColumnDTO> queryTableFieldList(@RequestBody @Validated ProcessDataSourceQueryTableMetaListRequest assertUserUse){
        return datasourceBizService.queryTableFieldList(assertUserUse);
    }

    @PostMapping("/query_table_field_model_list")
    public Map<String, Object> queryTableFieldModelList(@RequestBody @Validated ProcessDataSourceQueryTableMetaListRequest assertUserUse){
        return datasourceBizService.queryTableFieldModelList(assertUserUse);
    }


    @PostMapping("/query_create_table_field_list")
    public String queryCreateTableFieldList(@RequestBody @Validated ProcessDataSourceQueryCreateTableMetaListRequest assertUserUse){
        return datasourceBizService.queryCreateTableFieldList(assertUserUse);
    }
    @PostMapping("/query_update_table_field_list")
    public String queryUpdateTableFieldList(@RequestBody @Validated ProcessDataSourceQueryUpdateTableMetaListRequest assertUserUse){
        return datasourceBizService.queryUpdateTableFieldList(assertUserUse);
    }



    @PostMapping("/execute_ddl")
    public Boolean executeDdl(@RequestBody @Validated ProcessDataSourceExecuteDdlRequest assertUserUse){
        return datasourceBizService.executeDdl(assertUserUse);
    }

    @PostMapping("/query_table_index_list")
    public List<TableIndexDTO> queryTableIndexList(@RequestBody @Validated ProcessDataSourceQueryTableMetaListRequest assertUserUse){
        return datasourceBizService.queryTableIndexList(assertUserUse);
    }

    @PostMapping("/add_update_table")
    public Boolean addUpdateTable(@RequestBody @Validated ProcessDataSourceAddUpdateTableRequest assertUserUse){
        return datasourceBizService.addUpdateTable(assertUserUse);
    }

    @PostMapping("/add_update_index")
    public Boolean addUpdateIndex(@RequestBody @Validated ProcessDataSourceAddUpdateIndexRequest assertUserUse){
        return datasourceBizService.addUpdateIndex(assertUserUse);
    }


    @PostMapping("/drop_index")
    public Boolean dropIndex(@RequestBody @Validated ProcessDataSourceDropIndexRequest assertUserUse){
        return datasourceBizService.dropIndex(assertUserUse);
    }

    @PostMapping("/remove_table")
    public Boolean removeTable(@RequestBody @Validated ProcessDataSourceAddUpdateTableRequest assertUserUse){
        return datasourceBizService.removeTable(assertUserUse);
    }
    @PostMapping("/query_keys_list")
    public List<TableKeysDTO> queryKeysList(@RequestBody @Validated ProcessDataSourceQueryTableMetaListRequest assertUserUse){
        return datasourceBizService.queryKeysList(assertUserUse);
    }



    @PostMapping("/query_table_ddl")
    public String queryTableDdl(@RequestBody @Validated ProcessDataSourceQueryTableDdlRequest assertUserUse){
        return datasourceBizService.queryTableDdl(assertUserUse);
    }


    @PostMapping("/run")
    public SqlQueryResultDTO run(@RequestBody @Validated ProcessDataSourceRunRequest assertUserUse){
        return datasourceBizService.run(assertUserUse);
    }





    @PostMapping("/supports_catalogs")
    public Boolean supportsCatalogs(@RequestBody @Validated ProcessDataSourceDTO assertUserUse){
        return datasourceBizService.supportsCatalogs(assertUserUse);
    }
}
